﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseCore
{
    public class UserDao : IUserDao
    {
        public List<tbl_User> GetAllUser(bool active = true)
        {
            using (var ctx = new ExportDBEntities())
            {
                var users = (from x in ctx.tbl_User
                           //where x.Active == active
                           select x).ToList();
                var roles = from r in ctx.tbl_Role
                           select r;

                foreach (var u in users)
                {
                    foreach (var r in roles)
	                {
                        if (u.Role == r.Id)
                        {
                            u.RoleString = r.Name;
                        }
	                }
                }
                if (users == null)
                    return null;

                return users;
            }
        }

        public tbl_User GetUserByUserName(string username)
        {
            using (var ctx = new ExportDBEntities())
            {
                var user = (from x in ctx.tbl_User
                            where x.UserName == username
                            select x).FirstOrDefault();

                if (user == null)
                    return null;

                return user;
            }
        }

        public tbl_User Login(string userName, string password)
        {
            using (var ctx = new ExportDBEntities())
            {
                var user = (from x in ctx.tbl_User
                            join r in ctx.tbl_Role
                            on x.Role equals r.Id
                            where x.UserName == userName
                                  && x.Password == password
                            select new { UserName = x.UserName,
                            Password = x.Password,
                            RoleString = r.Name,
                            FullName= x.FullName,
                            Email = x.Email,
                            Role = x.Role,
                            Telephone = x.Telephone}).FirstOrDefault();

                if (user == null)
                    return null;
                tbl_User u = new tbl_User()
                {
                    UserName = user.UserName,
                    FullName = user.Password,
                    RoleString = user.RoleString,
                    Email = user.Email,
                    Telephone = user.Telephone,
                    Role =user.Role
                };
               
                return u;
            }
        }

        public bool UpdatePassword(string username, string password)
        {
            using (var ctx = new ExportDBEntities())
            {
                var user = (from x in ctx.tbl_User
                            where x.UserName == username
                            select x).FirstOrDefault();

                if (user == null)
                    return false;

                user.Password = password;
                return ctx.SaveChanges() > 0;
            }
        }

        public bool UpdatePinCode(int id, string pinCode)
        {
            using (var ctx = new ExportDBEntities())
            {
                var user = (from x in ctx.tbl_User
                            where x.Id == id
                            select x).FirstOrDefault();

                if (user == null)
                    return false;

                user.PinCode = pinCode;
                ctx.SaveChanges();
                return true;
            }
        }

        public bool Insert(tbl_User entity)
        {
            using (var ctx = new ExportDBEntities())
            {
                ctx.tbl_User.Add(entity);
                return ctx.SaveChanges() > 0;
            }
        }

        public bool Update(tbl_User entity)
        {
            using (var ctx = new ExportDBEntities())
            {
                tbl_User user = (from x in ctx.tbl_User
                                 where x.UserName == entity.UserName
                                 select x).FirstOrDefault();

                if (user == null)
                    return false;

                //user.UserName = entity.UserName;
                user.Email = entity.Email;
                user.FullName = entity.FullName;
                user.Password = entity.Password;
                user.Role = Convert.ToInt32(entity.Role);
                user.Telephone = entity.Telephone;
                user.Fax = entity.Fax;
                user.Active = entity.Active;
                user.PinCode = entity.PinCode;

                return ctx.SaveChanges() > 0;
            }
        }

        public bool Delete(string username)
        {
            using (var ctx = new ExportDBEntities())
            {
                tbl_User user = (from x in ctx.tbl_User
                                 where x.UserName == username
                                   select x).FirstOrDefault();

                if (user == null)
                    return false;

                ctx.tbl_User.Remove(user);
                return ctx.SaveChanges() > 0;
            }
        }


        public bool Delete(int id)
        {
            throw new NotImplementedException();
        }
    }
}
